package com.perceptivesoftware.licensing.client.dialogs.useradmin;

import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelIconProvider;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreeStyle;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

public class SecurityManagerTreePanel extends Composite {

	private final ContentPanel contentPanel = new ContentPanel();
	private final TreePanel<SecurityManagerNavModel> treePanel;

	public SecurityManagerTreePanel(final TreeStore<SecurityManagerNavModel> ts) {
		treePanel = new TreePanel<SecurityManagerNavModel>(ts);
		treePanel.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		treePanel.setIconProvider(new ModelIconProvider<SecurityManagerNavModel>() {

			public AbstractImagePrototype getIcon(final SecurityManagerNavModel model) {
				final String iconStyle = model.getIconStyleName();
				return IconHelper.create(iconStyle);
			}
		});
		contentPanel.setHeading("Navigation");
		final Button expandAll = new Button();
		expandAll.setToolTip("Expand All");
		expandAll.setIconStyle("perceptive-icon-expand");
		expandAll.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(final ButtonEvent ce) {
				for (final SecurityManagerNavModel child : treePanel.getStore().getModels()) {
					treePanel.setExpanded(child, true, true);
				}
			}
		});
		// contentPanel.getHeader().addTool(expandAll);
		contentPanel.setWidth("100%");
		contentPanel.setHeight("100%");
		contentPanel.setScrollMode(Scroll.AUTO);
		contentPanel.setStyleAttribute("backgroundColor", "white");
		contentPanel.setLayout(new FitLayout());
		treePanel.setWidth("100%");
		treePanel.setHeight("100%");
		final TreeStyle style = new TreeStyle();
		style.setLeafIcon(null);
		style.setNodeCloseIcon(null);
		style.setNodeOpenIcon(null);
		treePanel.setStyle(style);
		contentPanel.add(treePanel);
		initComponent(contentPanel);
	}

	public void setEHTreeContextMenu(final Menu menu) {
		treePanel.setContextMenu(menu);
	}

	public TreePanel<SecurityManagerNavModel> getTreePanel() {
		return treePanel;
	}
}
